﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AuctionSite.App_Code.DAL;
using System.Data.SqlClient;
using System.Data;
using System.Web.Security;

namespace AuctionSite.Usuarios
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string login = txtLogin.Text;
            string senha = FormsAuthentication.HashPasswordForStoringInConfigFile(txtSenha.Text.ToString(), "sha1");
            
            //obtem a string de conexão do arquivo web.config
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = Dados.StringDeConexao;

            //command
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;

            // cria um objeto Command
            cmd.CommandText = "SELECT * FROM usuarios where usu_email='" + login + "' AND usu_senha='" + senha + "' ";
            // abre a conexão com o banco de dados
            cn.Open();            
            SqlDataReader result = cmd.ExecuteReader();
            if (result.Read())
            {
                Session["id"] = result.GetSqlValue(0);
                Session["nome"] = result.GetSqlValue(1);
                Session["apelido"] = result.GetSqlValue(2);
                Session["email"] = result.GetSqlValue(7);
                Response.Write(Session["nome"]);
                Response.Redirect("Usuarios/Default.aspx");
            }
            else
            {
                Response.Write(
                    "<script type='text/javascript'>" +
                        "alert ('Dados incorretos!');" +
                        "window.location.href='Login.aspx';" +
                     "</script>"
                );
            }
            cn.Close();
        }
    }
}